import { defineStore } from "pinia";
import type { MajorStore } from "./types/type";
import type { addMajorResponseData, deleteMajorResponseData, listMajorResponseData, majorInfoData, pageMajorData, pageMajorResponseData, totalAndData } from "@/api/major/type";
import { reqAddMajor, reqAllMajor, reqDeleteMajor, reqMajorFindById, reqPageMajor, reqUpdateMajor } from "@/api/major";
import type { registerRuntimeCompiler } from "vue";

const majorStore = defineStore("Major",{
    state:():MajorStore=>{
        return{
            
        }
    },
    actions:{
        async addMajor(data:majorInfoData){
            let result:addMajorResponseData = await reqAddMajor(data);
            if(result.status == 0){
                const major = result.data as majorInfoData;
                return major;
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
        async findById(_id:string){
            let result:addMajorResponseData = await reqMajorFindById(_id);
            if(result.status == 0){
                const major = result.data as majorInfoData;
                return major;
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
        async updateMajor(major:majorInfoData){
            const result:addMajorResponseData = await reqUpdateMajor(major);
            if(result.status == 0){
                const major = result.data as majorInfoData;
                return major
            }else{
                return Promise.reject(new Error(result.msg));
            }
        },
        async allMajor(){
            const result:listMajorResponseData = await reqAllMajor();
            if(result.status == 0){
                const list = result.data as majorInfoData[];
                return list;
            }else{
                return Promise.reject(new Error(result.msg));
            }
        },
        async pageMajor(data:pageMajorData){
            const result:pageMajorResponseData = await reqPageMajor(data);
            if(result.status == 0){
                const page = result.data as totalAndData;
                return page;
            }else{
                return Promise.reject(new Error(result.msg));
            }
        },
        async deleteMajor(_id:string){
            const result:deleteMajorResponseData = await reqDeleteMajor(_id);
            if(result.status == 0){
                return "ok"
            }else{
                return Promise.reject(new Error(result.msg));
            }
        }
    }
})

export default majorStore;